<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    title="查看"
    width="500px"
    :maskClosable="true"
  >
    <BasicForm @register="registerForm">
      <template #json="{ model, field }">
        <CodeEditor v-model:value="model[field]" mode="application/json" readonly="true" />
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from './log.data';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { CodeEditor } from '/@/components/CodeEditor';

  export default defineComponent({
    name: 'LogDrawer',
    components: { BasicDrawer, BasicForm, CodeEditor },
    emits: ['success', 'register'],
    setup(_) {
      const isUpdate = ref(true);

      const [registerForm, { resetFields, setFieldsValue }] = useForm({
        labelWidth: 100,
        schemas: formSchema,
        showActionButtonGroup: false,
        disabled: true,
      });

      const [registerDrawer, { setDrawerProps }] = useDrawerInner(async (data) => {
        resetFields();
        setDrawerProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          setFieldsValue({
            ...data.record,
          });
        }
      });
      return { registerDrawer, registerForm };
    },
  });
</script>
